package com.alan.libapi.business.dao;

import com.alan.libapi.api.ApiResponse;

/**
 * BaseAsyncTaskThread 基础的一部任务类
 * @author JajuanWang
 * @date 2015-09-02 17:45
 * @version V1.0
 */
public abstract class BaseAsyncTaskThread<T> extends AsyncTaskThread {
	
	private CallbackListener<T> callbackListener;
	
	public BaseAsyncTaskThread(CallbackListener<T> callbackListener) {
		super();
		this.callbackListener = callbackListener;
	}

	@Override
	public void run() {
		ApiResponse<T> apiResponse = doInBackground();
		if (getCallbackListener() != null) {
			if (apiResponse.getRetCode() == ApiResponse.SUCCESS_CODE) {
				getCallbackListener().onSuccess(apiResponse.getResult());
			}else {
				getCallbackListener().onFailure(
						apiResponse.getErrorResponse().getErrorCode(),
						apiResponse.getErrorResponse().getErrorMsg());
			}
		}
	}
	
	/**
	 * 需要异步执行的任务
	 * @return
	 */
	protected abstract ApiResponse<T> doInBackground();
	public CallbackListener<T> getCallbackListener() {
		return callbackListener;
	}

	public void setCallbackListener(CallbackListener<T> callbackListener) {
		this.callbackListener = callbackListener;
	}

}
